let Common = {
    //返回值为一个数组
    dateType: function (val, callback) {
        if (val === '' && val !== 0) return;
        let sTime, eTime,
            nowTime = new Date().format('yy-mm-dd'),
            nowYear = new Date().getFullYear(),
            nowMon = new Date().getMonth() + 1,
            result;
        switch (val) {
            case 0://当天
                this.dataVaule = [];
                sTime = eTime = nowTime;
                result = [sTime, eTime];
                break;
            case 1://本周
                this.dataVaule = [];
                eTime = nowTime;
                let week = new Date().getDay() === 0 ? 6 : (new Date().getDay() - 1);
                let s = new Date(nowTime.replace(/-/g, '/')).getTime() - week * 86400000;
                sTime = new Date(s).format('yy-mm-dd');
                eTime = new Date(s + 6 * 86400000).format('yy-mm-dd');
                result = [new Date(sTime).format('yy-mm-dd'), eTime]
                break;
            case 2://本月
                this.dataVaule = [];
                sTime = nowYear + '-' + nowMon + '-01';
                if (nowMon === 1 || nowMon === 3 || nowMon === 5 || nowMon ===7 || nowMon === 8 || nowMon === 10 || nowMon === 12) {
                    eTime = nowYear + '-' + nowMon + '-31';
                } else if (nowMon === 2) {
                    let e = new Date(nowYear + '/03/01').getTime() - 86400000;
                    eTime = new Date(e).format('yy-mm-dd');
                } else {
                    eTime = nowYear + '-' + nowMon + '-30';
                }
                result = [new Date(sTime).format('yy-mm-dd'), new Date(eTime).format('yy-mm-dd')]
                break;
            case 3://本季
                this.dataVaule = [];
                eTime = nowTime;
                if (nowMon % 3 === 1) {
                    sTime = nowYear + '-' + nowMon + '-01';
                    let nextMon = nowMon + 3 === 13 ? (nowYear + 1) + '/01/01' : nowYear + '/' + (nowMon + 3) + '/01';
                    let e = new Date(nextMon).getTime() - 86400000;
                    eTime = new Date(e).format('yy-mm-dd');
                } else if (nowMon % 3 === 2) {
                    sTime = nowYear + '-' + (nowMon - 1) + '-01';
                    let nextMon = nowMon + 2 === 13 ? (nowYear + 1) + '/01/01' : nowYear + '/' + (nowMon + 2) + '/01';
                    let e = new Date(nextMon).getTime() - 86400000;
                    eTime = new Date(e).format('yy-mm-dd');
                } else {
                    sTime = nowYear + '-' + (nowMon - 2) + '-01';
                    let nextMon = nowMon === 12 ? (nowYear + 1) + '/01/01' : nowYear + '/' + (nowMon + 1) + '/01';
                    let e = new Date(nextMon).getTime() - 86400000;
                    eTime = new Date(e).format('yy-mm-dd');
                }
                result = [new Date(sTime).format('yy-mm-dd'), eTime]
                break;
            case 4://本年
                this.dataVaule = [];
                sTime = nowYear + '-01-01';
                eTime = nowYear + '-12-31';
                result = [sTime, eTime]
                break;
            default:
                result = callback(val);
        }
        return result;
    },
    //base64编码
    base64Encode: function (val) {
        let rv;
        rv = encodeURIComponent(val);
        rv = unescape(rv);
        rv = window.btoa(rv);
        return rv;
    },
    //复制一个与a相同的新对象（深度拷贝）
    cloneObj: function (a, b) {
        try {
            for (let attr in a) {
                b[attr] = a[attr];
            }
        } catch (err) {
            return;
        }
    },
}

/* 将数字日期格式化 */
Date.prototype.format = function (format) {
    let o = {
        "Y": this.getFullYear(),
        "M": this.getMonth() + 1, //month
        "d": this.getDate(), //day
        "h": this.getHours(), //hour
        "m": this.getMinutes(), //minute
        "s": this.getSeconds(), //second
        "q": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    o.M < 10 ? o.M = '0' + o.M : '';
    o.d < 10 ? o.d = '0' + o.d : '';
    o.h < 10 ? o.h = '0' + o.h : '';
    o.m < 10 ? o.m = '0' + o.m : '';
    o.s < 10 ? o.s = '0' + o.s : '';
    switch (format) {
        case 'yy-mm-dd':
            return o.Y + '-' + o.M + '-' + o.d;
            break;
        case 'hh-mm-ss':
            return o.h + ':' + o.m + ':' + o.s;
            break;
        case 'yy-mm':
            return o.Y + '年' + o.M + '月';
            break;
        default:
            return o.Y + '-' + o.M + '-' + o.d + ' ' + o.h + ':' + o.m + ':' + o.s;
    }
}


export default  Common;